package main

import (
	"fmt"
	"math/rand"
	"time"
)

// 空调结构体定义
type AirConditioner struct {
	ID         string
	Location   string
	IsAvailable bool
}

// 每个高温点的状态
type HighTempStatus struct {
	LastACSuggestTime   time.Time // 上次空调建议时间
	LastChillerTime     time.Time // 上次冷源建议时间
}

type ChillerSystem struct {
	LastSuggestTime time.Time
	CurrentTemp     float64
}

type RiskTraceRecord struct {
	ACLoadPercent float64
	LastSuggest   time.Time
}

// 判断是否需要处理高温点
func shouldHandle(status HighTempStatus, currentTemp float64, now time.Time) bool {
	// 温度低于阈值，不处理
	if currentTemp <= 31.0 {
		return false
	}
	// 空调或冷源建议间隔时间小于策略间隔，不处理
	if now.Sub(status.LastACSuggestTime) < 30*time.Minute && now.Sub(status.LastChillerTime) < 2*time.Hour {
		return false
	}
	return true
}

// 从空调列表中选择可用的空调
func selectAvailableACs(acs []AirConditioner, location string) []AirConditioner {
	var available []AirConditioner
	for _, ac := range acs {
		if ac.Location == location && ac.IsAvailable {
			available = append(available, ac)
		}
	}
	return available
}

// 冷源优化建议逻辑
func suggestChillerCooling(point string, status *HighTempStatus, now time.Time) {
	// 2 小时内未建议过冷源，则建议一次
	if now.Sub(status.LastChillerTime) > 2*time.Hour {
		fmt.Printf("✅ 对点位 %s 生成冷源优化建议：建议将供水温度下调 1℃\n", point)
		status.LastChillerTime = now
	}
}

// 风险回溯建议逻辑（如空调负荷很低但问题依旧）
func riskTrace(acLoad float64, lastSuggest time.Time, now time.Time) {
	// 空调负荷低于 60%，并且超过 6 小时未建议回溯
	if acLoad < 60.0 && now.Sub(lastSuggest) > 6*time.Hour {
		fmt.Println("⚠️ 风险回溯建议：空调负荷较低但环境未改善，请复核调控策略")
	}
}

// 高温点主处理逻辑
func handleHighTempPoint(point string, currentTemp float64, status *HighTempStatus, acs []AirConditioner, now time.Time, acLoad float64) {
	if !shouldHandle(*status, currentTemp, now) {
		fmt.Println("➡️ 当前无需处理高温点:", point)
		riskTrace(acLoad, status.LastACSuggestTime, now)
		return
	}

	// 选择可用空调
	availableACs := selectAvailableACs(acs, point)
	if len(availableACs) == 0 {
		fmt.Println("🚫 无可用空调，考虑冷源优化:", point)
		suggestChillerCooling(point, status, now)
		riskTrace(acLoad, status.LastChillerTime, now)
		return
	}

	// 建议前两个空调下调设定温度
	fmt.Println("🌀 可用空调数:", len(availableACs), "点位:", point)
	for i, ac := range availableACs {
		if i >= 2 {
			break
		}
		fmt.Printf("✅ 对空调 %s 下发降温建议\n", ac.ID)
	}
	status.LastACSuggestTime = now
}



func main() {
	now := time.Now()

	// 创建 30 台空调，均匀分布在 A1~A3 区域
	var acs []AirConditioner
	locations := []string{"A1", "A2", "A3"}
	for i := 1; i <= 30; i++ {
		location := locations[(i-1)%len(locations)]
		acs = append(acs, AirConditioner{
			ID:          fmt.Sprintf("AC%02d", i),
			Location:    location,
			IsAvailable: rand.Intn(100) >= 25, // 约 75% 可用
		})
	}

	// 随机生成 2~4 个高温点
	numHighTempPoints := rand.Intn(3) + 2
	pointTemps := make(map[string]float64)
	highTempPoints := make(map[string]*HighTempStatus)

	used := map[string]bool{}
	for len(pointTemps) < numHighTempPoints {
		loc := locations[rand.Intn(len(locations))]
		if used[loc] {
			continue
		}
		used[loc] = true

		temp := 30.0 + rand.Float64()*4.5 // 温度范围：30.0 ~ 34.5
		pointTemps[loc] = temp

		highTempPoints[loc] = &HighTempStatus{
			LastACSuggestTime: now.Add(time.Duration(-rand.Intn(60)) * time.Minute),  // 上次空调建议时间：0~60分钟前
			LastChillerTime:   now.Add(time.Duration(-rand.Intn(240)) * time.Minute), // 上次冷源优化时间：0~240分钟前
		}
	}

	// 根据空调数量和启用率估算负荷率（模拟 40%~95%）
	activeCount := 0
	for _, ac := range acs {
		if ac.IsAvailable {
			activeCount++
		}
	}
	acLoad := 40.0 + rand.Float64()*55.0 // 范围 40~95%

	fmt.Printf("🏭 总空调数: %d, 可用数: %d\n", len(acs), activeCount)
	fmt.Printf("📊 当前空调负荷率: %.1f%%\n\n", acLoad)

	fmt.Println("🔥 高温点处理逻辑开始...\n")
	for point, temp := range pointTemps {
		status := highTempPoints[point]
		handleHighTempPoint(point, temp, status, acs, now, acLoad)
		fmt.Println()
	}
}
